package controllers

import (
	"context"
	"github.com/goravel/framework/contracts/http"
	"github.com/goravel/framework/facades"
	"goravel/app/models"
	userregisterproto "goravel/app/protos/users"
)

type RegisterController struct {
	//Dependent services
}

func NewRegisterController() *RegisterController {
	return &RegisterController{}
}

func (r *RegisterController) UserRegister(ctx context.Context, req *userregisterproto.UserRegisterRequest) (*userregisterproto.UserRegisterResponse, error) {
	user := req.User
	if err := facades.Orm().Query().Where("mobile = ?", user.GetMobile()).First(&user); err != nil {
		return &userregisterproto.UserRegisterResponse{
			Code:    http.StatusUnprocessableEntity,
			Message: "用户已存在",
		}, nil
	}
	modeluser := models.User{}
	modeluser.Mobile = user.GetMobile()
	modeluser.Password = user.GetPassword()
	if err_c := facades.Orm().Query().Create(&modeluser); err_c != nil {
		return &userregisterproto.UserRegisterResponse{
			Code:    http.StatusUnprocessableEntity,
			Message: "注册失败",
		}, nil
	}
	return &userregisterproto.UserRegisterResponse{
		Code:    http.StatusOK,
		Message: "注册成功",
	}, nil
}
